package com.lin.controller;

import com.lin.annotation.MyLog;
import com.lin.pojo.*;
import com.lin.services.CategoryService;
import com.lin.services.WaresService;
import com.lin.utils.PageUnit;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * ClassName:WaresController
 * Package:com.lin.controller
 * Author: hellcat
 * Date: 2019/12/19
 * ♣♣♣  商品管理
 */

@Controller
@RequestMapping("/wares")
public class WaresController {
    @Autowired
    private WaresService waresService;
    @Autowired
    private CategoryService categoryService;

    @MyLog("查询商品信息")
    @RequiresAuthentication
    @RequiresPermissions("wares:wares:list")
    @RequestMapping(value = "/toWares", method = RequestMethod.GET)
    public String toWares(HttpServletRequest request) {
        request.setAttribute("ddId", "Wares");
        List<Category> list1 = categoryService.getCategory1();
        request.setAttribute("Category1", list1);
        return "waresMain";
    }

    @RequiresPermissions("wares:wares:list")
    @RequestMapping(value = "/getCategory2", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getCategory2(HttpServletRequest request) {
        String firstCategoryId = request.getParameter("firstCategoryId");
        return categoryService.getCategory2(Integer.parseInt(firstCategoryId));
    }

    @RequiresPermissions("wares:wares:list")
    @RequestMapping(value = "/getWarsList", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getWarsList(HttpServletRequest request) {
        String id = request.getParameter("id");
        String category1 = request.getParameter("category1");
        String category2 = request.getParameter("category2");
        String status = request.getParameter("waresStatus");
        int page = Integer.parseInt(request.getParameter("page"));
        int limit = Integer.parseInt(request.getParameter("limit"));
        List<Wares> list = waresService.getWarsList(id, category1, category2, status);
        return PageUnit.Page(list, page, limit);
    }

    @MyLog("删除商品")
    @RequiresPermissions("wares:wares:delete")
    @RequestMapping(value = "/deleteWares", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> deleteWares(HttpServletRequest request) {
        String id = request.getParameter("id");
        return waresService.deleteWares(id);

    }

    @MyLog("创建新的商品单位")
    @RequiresPermissions("wares:wares:list")
    @RequestMapping(value = "/insertUnit", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> insertUnit(HttpServletRequest request) {
        String unit = request.getParameter("unit");
        return waresService.insertUnit(unit);

    }

    @RequiresPermissions("wares:wares:list")
    @RequestMapping(value = "/getUnit", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getUnit() {
        return waresService.findUnit();
    }

    @MyLog("修改商品信息")
    @RequiresPermissions("wares:wares:update")
    @RequestMapping(value = "/updateWares", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> updateWares(Wares wares) {
        return waresService.updateWares(wares);
    }

    @MyLog("下架商品")
    @RequiresPermissions("wares:wares:update")
    @RequestMapping(value = "/changeStatus", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> changeStatus(HttpServletRequest request) {
        String ids = request.getParameter("id_str");
        return waresService.changeStatus(ids);
    }

    @MyLog("添加新商品")
    @RequiresPermissions("wares:wares:insert")
    @RequestMapping(value = "/insertWares", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> insertWares(Wares wares) {
        return waresService.insertWares(wares);
    }

}
